import os
import requests
import time
import re
import csv

# 创建session对象
session = requests.Session()
url = 'https://quotes.toscrape.com/login'
data = {
    'username': 'mcl',
    'password': '123456'
}
session.post(url, data=data)

# 获得cookie
print(session.cookies)


root='yinwen'
if not os.path.exists(root):
    os.makedirs(root)

# url='https://quotes.toscrape.com/page/2/'
# res=session.get(url).content.decode('utf8')
# print(res)
for i in range(1, 11):
    res = session.get(f'https://quotes.toscrape.com/page/{i}/').content.decode()
    r = re.findall(r'<span class="text" itemprop="text">(.*?)</span>', res)
    print(f'正在写入第{i}页')
    with open(f'{root}/page_number_{i}', 'a', encoding='utf8',newline='') as file:
        for item in r:
            file.write(item + '\n')
    time.sleep(1)